package com.heo.protocol.packet;

import org.apache.log4j.Logger;

import com.heo.util.ByteHelper;

public class EditDevicePasswordReq extends BasePacket
{
  private int BODY_LENGTH = 4;// 包体长4个字节
  private static final String PACKET_NAME = "[修改设备密码请求包]";

  private String password = null;// 设备密码，4个字节

  private static Logger log = Logger.getLogger(EditDevicePasswordReq.class);

  /**
   * 通过网络传过来的流构建对象
   * 
   * @param totalLength
   * @param commandId
   * @param sequenceId
   * @param body
   * @throws Exception
   */
  public EditDevicePasswordReq(String logHeader, SocketPacket packet) throws Exception
  {
    super.copyPacket(logHeader, PACKET_NAME, packet);
    makeBody();
  }

  public EditDevicePasswordReq() throws Exception
  {
    super();
    super.commandId = DeviceCommand.EDIT_LOGIN_PASSWORD_REQ;
    super.sequenceId = 0x01;
    super.totalLength = HEAD_LENGTH + BODY_LENGTH;
  }

  @Override
  public int getTotalLength()
  {
    return super.HEAD_LENGTH + BODY_LENGTH;
  }

  @Override
  public byte[] getPacketBody()
  {
    return ByteHelper.margeByte(ByteHelper.stringToByteArray(password, 4), null, null, null, null, null, null, null, null, null);
  }

  @Override
  public void makeBody() throws Exception
  {
    if (packetBody.length != BODY_LENGTH)
    {
      log.error(super.logHeader + super.packetName + "数据包长度错误。包体长度应该为：" + BODY_LENGTH + "个字节。");
      throw new Exception(super.logHeader + super.packetName + "数据包长度错误。包体长度应该为：" + BODY_LENGTH + "个字节。");
    }
    password = ByteHelper.byteToString(packetBody, 0, 4, (byte) 0);
    log.debug(super.logHeader + super.packetName + "数据包解析成功。");
  }

  public static void main(String[] args)
  {
    try
    {
      GetDeviceParaReq gdpr = new GetDeviceParaReq();
      gdpr.setCommandId((short) 0x0005);
      gdpr.setDeviceMac("1122334455");
      gdpr.setDeviceSn("1111111111");
      gdpr.setHardVer("1.00");
      gdpr.setSendCount((short) 1);
      gdpr.setSequenceId(1);
      gdpr.setSimId("2222222222");
      System.out.println(gdpr.toString());
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  public String getPassword()
  {
    return password;
  }

  public void setPassword(String password)
  {
    this.password = password;
  }

}
